package com.fpt.edu.vn.h2tv.api.user;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fpt.edu.vn.h2tv.common.BookStoreUlti;
import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.TbAccountDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbAccount;
import com.fpt.edu.vn.h2tv.dto.UserInfo;
import com.fpt.edu.vn.h2tv.exception.NoPermissionException;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;


@Path("/")
public class BookStoreUserAPI {

    @Path("/changeAvatar")
    @POST
    @Consumes({MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_FORM_URLENCODED})
    public Response demo(@Context HttpServletRequest request,
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail,
            @FormDataParam("backendUrl") String backend) throws IOException{
        System.out.println(backend);
        String uploadedFileLocation = request.getSession().getServletContext().getRealPath("/avatars");
        String fileName = "/"+System.currentTimeMillis()+fileDetail.getFileName();
        System.out.println(uploadedFileLocation);
       // writeToFile(uploadedInputStream, uploadedFileLocation+fileName);
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(ElementName.USER_INFO);
        if(userInfo!=null){
            try{
              //  String fileName = "\\"+System.currentTimeMillis()+fileDetail.getFileName();
                BookStoreUlti.writeToFile(uploadedInputStream, uploadedFileLocation+fileName);
            TbAccountDAO dao = new TbAccountDAO();
            List<TbAccount> account = (List<TbAccount>) dao.findByUsername(userInfo.getUserName());
            if(account!=null&&account.size()>0){
                TbAccount realAccount = account.get(0);
                realAccount.setAvatar(backend+"avatars"+fileName);
                EntityManagerHelper.beginTransaction();
                dao.update(realAccount);
                EntityManagerHelper.commit();
                userInfo.setAvatar(realAccount.getAvatar());
                request.getSession().setAttribute(ElementName.USER_INFO, userInfo);
                return Response.ok(userInfo).build();
            }
            }catch(Exception ex){
                ex.printStackTrace();
            }
         }else{
             throw new NoPermissionException("use have no permission to access this API");
         }
        // save it
      
 
        //String output = "File uploaded to : " + uploadedFileLocation;
 
        return Response.status(200).entity("oke").build();
        
    }
    
  
}
